package duppentity.dto.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import constants.OrderDefault;
import utils.JSONObjectUtils;
import utils.JsonUtils;
import utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangzhi
 * @date 2019/8/1
 */
public class UserDtoUtil {

	public static UserOrderDto getUserOrderDto(JsonObject data) {
		UserOrderDto userOrderDto =new UserOrderDto();
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					JSONObject listOne = JSONObjectUtils.get(list, 0);
					userOrderDto = convertUserOrderDto(listOne,data,Boolean.TRUE);
				}
			}
		}
		return userOrderDto;
	}

	private static UserOrderDto convertUserOrderDto(JSONObject listOne,JsonObject data,Boolean isDetail){
		UserOrderDto userOrderDto = new UserOrderDto();
		userOrderDto.id = listOne.getString("id");
		userOrderDto.userId = listOne.getLongValue("userId");
		if (userOrderDto.userId>0){
			JSONObject user_info = JSONObjectUtils.get(data, "user_info");
			userOrderDto.userMobile =JSONObjectUtils.getString(user_info,listOne.getString("userId"),"mobile");
		}

		userOrderDto.carLicensePlateResultId = listOne.getLongValue("carLicensePlateResultId");
		if (userOrderDto.carLicensePlateResultId>0){
			JSONObject car_license_plate_result = JSONObjectUtils.get(data, "car_license_plate_result");
			JSONObject car_license_plate = JSONObjectUtils.get(data, "car_license_plate");
			JSONObject car_license_plate_result_dto=JSONObjectUtils.getJSONObject(car_license_plate_result,listOne.getString("carLicensePlateResultId"));
			userOrderDto.carNo =JSONObjectUtils.getString(car_license_plate,car_license_plate_result_dto.getString("carLicensePlateId"),"license");
			userOrderDto.carType = OrderDefault.getResultName(car_license_plate_result_dto.getIntValue("type"),OrderDefault.nameType.carType);
		}
		userOrderDto.stationId = listOne.getLongValue("stationId");
		if (userOrderDto.stationId>0){
			JSONObject bu_station = JSONObjectUtils.get(data, "bu_station");
			userOrderDto.stationName = JSONObjectUtils.getString(bu_station,listOne.getString("stationId"),"name");
		}

		userOrderDto.orderNo = listOne.getString("orderNo");
		userOrderDto.goodsId = listOne.getLongValue("goodsId");
		userOrderDto.goodsName = userOrderDto.stationName.concat("精洗服务");
		userOrderDto.price = Utils.getStringPrice(listOne.getIntValue("price"));
		userOrderDto.discountRate = Utils.getStringPrice(listOne.getIntValue("discountRate"),10);
		userOrderDto.payinId = listOne.getLongValue("payinId");
		JSONObject payin = JSONObjectUtils.get(data, "payin");
		JSONObject userCoupon = JSONObjectUtils.get(data, "user_coupon");
		if (userOrderDto.payinId>0){
			JSONObject payinDto = JSONObjectUtils.getJSONObject(payin,listOne.getString("payinId"));
			userOrderDto.amount = Utils.getStringPrice(payinDto.getIntValue("amount"));
			String userCouponId = payinDto.getString("userCouponId");
			userOrderDto.couponId = JSONObjectUtils.getInteger(userCoupon, userCouponId, "couponId");
			userOrderDto.discountAmount = Utils.getStringPrice((listOne.getIntValue("price") - payinDto.getIntValue("amount")));
			userOrderDto.payTypeName = OrderDefault.getResultName(payinDto.getIntValue("payType"), OrderDefault.nameType.payType);
			userOrderDto.payinTime =payinDto.getLongValue("payTime");
		}
		userOrderDto.status = listOne.getIntValue("status");
		userOrderDto.statusName = OrderDefault.getResultName(userOrderDto.status,OrderDefault.nameType.status);

		userOrderDto.failReason = listOne.getIntValue("failReason");
		userOrderDto.failReasonName = OrderDefault.getResultName(userOrderDto.failReason,OrderDefault.nameType.failReason);

		userOrderDto.refundAudit = listOne.getIntValue("refundAudit");
		userOrderDto.refundAuditName = OrderDefault.getResultName(userOrderDto.refundAudit,OrderDefault.nameType.refundAudit);

		userOrderDto.refundStatus = listOne.getIntValue("refundStatus");
		userOrderDto.refundStatusName = OrderDefault.getResultName(userOrderDto.refundStatus,OrderDefault.nameType.refundStatus);
		userOrderDto.enterTime =listOne.getLongValue("enterTime");
		userOrderDto.exitTime =listOne.getLongValue("exitTime");
		userOrderDto.startTime =listOne.getLongValue("startTime");
		userOrderDto.finishTime =listOne.getLongValue("finishTime");
		if (isDetail) {
			if (userOrderDto.refundStatus.equals(OrderDefault.refundStatus.two)) {
				//已退款
				userOrderDto.refundId = listOne.getLongValue("refundId");
				if (userOrderDto.refundId>0){
					JSONObject refund = JSONObjectUtils.get(data, "refund");
					JSONObject refundDto=JSONObjectUtils.getJSONObject(refund,listOne.getString("refundId"));
					String payinId=refundDto.getString("payinId");
					userOrderDto.refundTradeNo =refundDto.getString("refundTradeNo");
					userOrderDto.refundFee =Utils.getStringPrice(refundDto.getIntValue("refundFee"));
					userOrderDto.refundTime =refundDto.getLongValue("refundTime");

					userOrderDto.refundPayTypeName = OrderDefault.getResultName(JSONObjectUtils.getInteger(payin,payinId,"payType"),OrderDefault.nameType.payType);
				}
			}
		}
		userOrderDto.createTime = listOne.getLong("createTime");
		userOrderDto.modifyTime = listOne.getLong("modifyTime");
		return  userOrderDto;
	}



	public static List<UserOrderDto> getUserOrderDtoList(JsonObject data){
		List<UserOrderDto> userOrderDtoList = null;
		if (data!=null) {
			if (data.has("total")) {
				if (data.get("total").getAsInt() > 0) {
					JsonArray list = data.getAsJsonArray("list");
					if (list != null && list.size() > 0) {
						userOrderDtoList = new ArrayList<>();
						for (Integer i=0;i<list.size();i++) {
							JSONObject listOne = JSONObjectUtils.get(list, i);
							UserOrderDto adDto = convertUserOrderDto(listOne,data,Boolean.FALSE);
							userOrderDtoList.add(adDto);
						}
					}
				}
			}
		}
		return  userOrderDtoList;
	}
}
